﻿<sh:BasePage
    x:Class="CorpStore.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="using:CorpStore"
    xmlns:vm="using:CorpStore.ViewModels"
    xmlns:common="using:CorpStore.Common"
    xmlns:ctl="using:CorpStore.Controls"
    xmlns:sh="using:StoreHelper"
    RequestedTheme="Light"
    NavigationCacheMode="Enabled"
    Loaded="BasePage_Loaded"
    mc:Ignorable="d">

    <!-- set design time data context -->
    <Page.DataContext>
        <vm:FakeViewModel/>
    </Page.DataContext>

    <!-- Set of resources local to this page (CollectionViewSource, DataTemplates, etc.) -->
    <Page.Resources>
        <!-- Collection of items displayed by this page -->
        <CollectionViewSource
            x:Name="applicationsViewSource"
            Source="{Binding GroupList.Groups}"
            IsSourceGrouped="true"
            ItemsPath="Applications"/>

        <CollectionViewSource 
            x:Name="featuredAppsViewSource" 
            Source="{Binding GroupList.FeaturedApps}"/>

        <!-- data template used for group headers on MainPage -->
        <DataTemplate x:Key="GroupHeaderTemplate">
            <Grid x:Name="headerRoot" Margin="0,0,0,2">
                <Button Click="Header_Click" 
                    AutomationProperties.Name="Group Title"
                    Style="{StaticResource TextBlockButtonStyle}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" Margin="0,-11,10,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
                        <TextBlock Text="&#xE26B;" FontFamily="Segoe UI Symbol" Margin="0,-11,0,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
                    </StackPanel>
                </Button>
            </Grid>
        </DataTemplate>


        <DataTemplate x:Key="fvItemTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="200"/>
                </Grid.RowDefinitions>
                
                <Image Grid.Row="0" x:Name="imgScreenshot" Source="{Binding FirstScreenshotImage}" Stretch="Uniform" Margin="20" Tapped="imgScreenshot_Tapped" HorizontalAlignment="Center" VerticalAlignment="Center" MinHeight="100" />
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="5*"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" Margin="0,10,10,0" HorizontalAlignment="Right" VerticalAlignment="Top" BorderThickness="3" BorderBrush="Black" Height="100" Width="100">
                        <Image x:Name="imgLogo" Source="{Binding LogoImage}"  Stretch="Fill"/>
                    </Border>
                    <Grid Grid.Column="1" VerticalAlignment="Top" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="{Binding DisplayName}" Style="{StaticResource SubheaderTextBlockStyle}" Margin="0,0,0,4" FontSize="24"/>
                        <ScrollViewer Grid.Row="1" Padding="0,0,16,0" ViewChanged="ScrollViewer_ViewChanged" >
                            <TextBlock Text="{Binding Description}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="12" TextWrapping="Wrap"/>
                        </ScrollViewer>
                    </Grid>
                </Grid>
            </Grid>
        </DataTemplate>
        
        <!-- converters -->
        <sh:NotBoolToVisibilityConverter x:Key="NotBoolToVisConverter"/>
    </Page.Resources>

    <Page.BottomAppBar>
        <CommandBar Background="Black" IsSticky="False">
            <AppBarButton Name="btnRebuildCatalog" Label="Rebuild" Background="Green" Icon="Repair" BorderBrush="Green" Click="btnRebuildCatalog_Click"/>
            <AppBarButton Name="btnRefresh" Label="Refresh" Background="Green" Icon="Refresh" BorderBrush="Green" Click="btnRefresh_Click"/>
        </CommandBar>
    </Page.BottomAppBar>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}" >
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Rectangle Grid.RowSpan="2" Fill="#7f000000" Visibility="{Binding GroupsExist, Converter={StaticResource NotBoolToVisConverter}}" />

        <!-- Row 0: Back button and page title -->
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="275"/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{Binding CorpStoreTitle}" Style="{StaticResource HeaderTextBlockStyle}" 
                       IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,0,40"/>
            <Button Width="1"/>
            <SearchBox x:Name="SearchBoxSuggestions" Grid.Column="2" Height="35" 
                   HorizontalAlignment="Left" VerticalAlignment="Center" Width="250" SuggestionsRequested="SearchBoxEventsSuggestionsRequested" 
                   ResultSuggestionChosen="SearchBoxSuggestions_ResultSuggestionChosen" IsTabStop="False" PlaceholderText="Search Apps" SearchHistoryEnabled="False" FocusOnKeyboardInput="True"/>

        </Grid>

        <!-- Row 1: Main content -->
        <Grid Grid.Row="1" Margin="39,0,0,0" x:Name="ContentGrid">
            <ProgressRing x:Name="progressRing" IsActive="{Binding ProgressIsActive}" Width="50" Height="50"/>
            <GridView x:Name="gvPanorama" Grid.Column="1" ScrollViewer.HorizontalScrollBarVisibility="Visible" Style="{StaticResource PanoramaGridViewStyle}" VerticalContentAlignment="Top" SelectionMode="None" >
                <GridViewItem x:Name="gviFlip" Margin="0,0,60,0" VerticalAlignment="Top" Width="500" IsTabStop="False" >
                    <Grid VerticalAlignment="Top">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Featured Apps" Margin="6,0,0,8" Style="{StaticResource SubheaderTextBlockStyle}" />
                        <Border Grid.Row="1" BorderThickness="3" CornerRadius="10" Margin="0,0,0,16" BorderBrush="Black">
                            <Grid>
                                <FlipView x:Name="flipView" ItemsSource="{Binding Source={StaticResource featuredAppsViewSource}}" ItemTemplate="{StaticResource fvItemTemplate}" SelectedIndex="{Binding SelectedFlipIndex, Mode=TwoWay}" SelectionChanged="flipView_SelectionChanged" />
                            </Grid>
                        </Border>
                    </Grid>
                </GridViewItem>

                <!-- Horizontal scrolling grid used in most view states -->
                <GridViewItem VerticalAlignment="Top" Margin="0,8,0,0" >
                    <GridView
                    x:Name="itemGridView"
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Grouped Items"
                    ItemsSource="{Binding Source={StaticResource applicationsViewSource}}"
                    SelectionMode="None"
                    Grid.Column="1"
                    IsItemClickEnabled="True"
                    ItemClick="ItemView_ItemClick" >

                        <GridView.ItemTemplate>
                            <!-- Grid-appropriate 250 pixel square item template as seen in the GroupedItemsPage and ItemsPage -->
                            <DataTemplate>
                                <Grid HorizontalAlignment="Left" Width="200" Height="200">
                                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                                        <Image Source="{Binding SplashScreenImage}" Stretch="Fill" AutomationProperties.Name="{Binding Name}"/>
                                    </Border>
                                    <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                                        <TextBlock Text="{Binding DisplayName}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" Margin="15,0,15,0" FontWeight="SemiBold"/>
                                        <TextBlock Text="{Binding ShortDescription}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="Wrap" Margin="15,0,15,10" FontSize="12"/>
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </GridView.ItemTemplate>

                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <ItemsWrapGrid x:Name="rootPanel" GroupPadding="0,0,70,0"/>
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>

                        <GridView.GroupStyle>
                            <GroupStyle HeaderTemplate="{StaticResource GroupHeaderTemplate}">
                                <GroupStyle.Panel>
                                    <ItemsPanelTemplate>
                                        <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0" MaxWidth="600"/>
                                    </ItemsPanelTemplate>
                                </GroupStyle.Panel>
                            </GroupStyle>
                        </GridView.GroupStyle>
                    </GridView>
                </GridViewItem>

            </GridView>


            <!-- Vertical scrolling list only used when snapped -->
            <ListView
            x:Name="itemListView"
            AutomationProperties.AutomationId="ItemListView"
            AutomationProperties.Name="Grouped Items"
            Grid.Row="1"
            Visibility="Collapsed"
            Margin="0,-10,0,0"
            Padding="10,0,0,60"
            ItemsSource="{Binding Source={StaticResource applicationsViewSource}}"
            SelectionMode="None"
            IsSwipeEnabled="false"
            IsItemClickEnabled="True"
            ItemClick="ItemView_ItemClick">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="6">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="60" Height="60">
                                <Image Source="{Binding SplashScreenImage}" Stretch="Fill">
                                </Image>
                            </Border>
                            <StackPanel Grid.Column="1" Margin="8,0,0,0" Orientation="Horizontal">
                                <TextBlock Text="{Binding DisplayName}" Margin="0,-11,10,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>

                <ListView.GroupStyle>
                    <GroupStyle HeaderTemplate="{StaticResource GroupHeaderTemplate}">
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid Orientation="Horizontal" Margin="0,0,80,0" MaxHeight="300"/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>

        </Grid>
        
        <!-- main content grid -->
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="Portrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="gvPanorama" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemListView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

    </Grid>
</sh:BasePage>
